/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 27.11.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework.resource;

import java.net.URI;

/**
 * This class represents a resource URI object.
 */
public class ResourceURI {
	
	private URI uri;
	private ResourceURINode header;
	private ResourceURINode trailer;
	
	/**
	 * Creates a resource URI.
	 * 
	 * @param uri The URI of the resource URI object.
	 */
	public ResourceURI(URI uri) {
		this.uri = uri;
		this.header = new ResourceURINode("");
		this.trailer = new ResourceURINode("");
		this.header.setNext(this.trailer);
		this.trailer.setPrev(this.header);
		this.parseUri(uri.getPath());
	}
	
	/**
	 * Gets the first node of the resource URI.
	 * 
	 * @return The first node of the resource URI.
	 */
	public ResourceURINode getFirstNode() {
		ResourceURINode nextURINode = this.header.getNext();
		if(nextURINode != this.trailer){
			return nextURINode;
		}
		else {
			return null;
		}
	}
	
	/**
	 * Gets the last node of the resource URI.
	 * 
	 * @return The last node of the resource URI.
	 */
	public ResourceURINode getLastNode() {
		ResourceURINode prevURINode = this.trailer.getPrev();
		if(prevURINode != this.header){
			return prevURINode;
		}
		else {
			return null;
		}
	}
	
	/**
	 * Gets the next node of a given resource URI node.
	 * 
	 * @param uriNode The given resource URI node whose next node is on demand.
	 * @return The next node of the given resource URI node.
	 */
	public ResourceURINode getNextNode(ResourceURINode uriNode) {
		if(uriNode.getNext() != this.trailer){
			return uriNode.getNext();
		}
		else {
			return null;
		}
	}
	
	/**
	 * Gets the previous node of a given resource URI node.
	 * 
	 * @param uriNode The given resource URI node whose previous node is on demand.
	 * @return The previous node of the given resource URI node.
	 */
	public ResourceURINode getPreviousNode(ResourceURINode uriNode) {
		if(uriNode.getPrev() != this.header){
			return uriNode.getPrev();
		}
		else {
			return null;
		}
	}
	
	/**
	 * Gets the URI object of the resource URI.
	 * 
	 * @return The URI object of the resource URI.
	 */
	public URI getURI() {
		return this.uri;
	}
	
	/**
	 * Gets the whole resource URI as string.
	 * 
	 * @return The resource URI.
	 */
	public String getResourceUri() {
		return this.uri.getPath();
	}
	
	/**
	 * Parses a given resource URI and writes its nodes to a string array.
	 * 
	 * @param resourceURI The resource URI to parse.
	 */
	private void parseUri(String resourceURI) {
		String uriNodes[];
		uriNodes = resourceURI.split("/");
		for (int i = 0; i < uriNodes.length; i++) {
			if(!uriNodes[i].isEmpty()){
				this.insertNode(uriNodes[i]);
			}
		}
	}
	
	/**
	 * Insert a resource URI node at the end of the resource URI.
	 * 
	 * @param uriNode The resource URI node to insert.
	 */
	public void insertNode(String uriNode) {
		if(uriNode != null){
			ResourceURINode resourceURINode = new ResourceURINode(uriNode);
			resourceURINode.setNext(this.trailer);
			this.trailer.getPrev().setNext(resourceURINode);
			resourceURINode.setPrev(this.trailer.getPrev());
			this.trailer.setPrev(resourceURINode);
		}
	}

	public String toString() {
		String resourceURI = "";
		ResourceURINode run = this.header;
		while((run = this.getNextNode(run)) != null){
			resourceURI += "/" + run.getUriNode();
		}
		return resourceURI;
	}
	
}
